package com.tabjin.rfidsocket.controller.v1.AoTu;

import com.alibaba.fastjson.JSONArray;
import com.tabjin.rfidsocket.common.utils.TabJsonResult;
import com.tabjin.rfidsocket.controller.v1.BaseController;
import com.tabjin.rfidsocket.pojo.vo.BindInfoVO;
import com.tabjin.rfidsocket.common.utils.TabJsonResultCM;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.tools.jconsole.Tab;

import java.io.*;

/**
 * @author tabjin
 * create at 2020/12/3 17:20
 * @program rfid_socket
 * @description 傲途设备绑定controller
 */
@RequestMapping(value = "/api")
@RestController
public class AutoController extends BaseController {

    final static Logger logger = LoggerFactory.getLogger(AutoController.class);

    @PostMapping("/Bind")
    @ResponseBody
//    public TabJsonResult bind(@RequestParam(value = "appkey") String appKey,
//                              @RequestParam(value = "secret") String appSecret,
//                              @RequestBody BindInfoVO bindInfoVO) {
//        if (StringUtils.isBlank(appKey) || StringUtils.isBlank(appSecret)) {
//            return TabJsonResult.errorMsg("appKey或appSecret不能为空");
//        }
//        // 校验
//        if (!StringUtils.equals(appKey, APP_KEY)) {
//            return TabJsonResult.errorMsg("appKey错误");
//        }
//        if (!StringUtils.equals(appSecret, APP_SECRET)) {
//            return TabJsonResult.errorMsg("appSecret错误");
//        }
    public TabJsonResultCM bind(@RequestBody BindInfoVO bindInfoVO) {
        if (bindInfoVO == null) {
            return TabJsonResultCM.errorMsg("未接收到绑定的数据");
        }
//        System.out.println("==========================================================");
//        System.out.println("相关信息：" + JSONArray.toJSON(bindInfoVO));
//        System.out.println("==========================================================");
        logger.info("==========================================================");
        logger.info("相关信息：\n{},\n{},\n{}\n", JSONArray.toJSON(bindInfoVO));
        logger.info("==========================================================");
        return TabJsonResultCM.ok();
    }

    @PostMapping("/upload")
    @ResponseBody
    public TabJsonResult upload(@RequestParam("image") MultipartFile file) {
        if (!file.isEmpty()) {
            try {
                /*
                 * 这段代码执行完毕之后，图片上传到了工程的跟路径
                 * 1、文件路径； 2、文件名；3、文件格式; 4、文件大小的限制;
                 */
                BufferedOutputStream out = new BufferedOutputStream(
                        new FileOutputStream(new File(
                                file.getOriginalFilename())));
                System.out.println(file.getName());
                out.write(file.getBytes());
                out.flush();
                out.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return TabJsonResult.errorMessage("上传失败," + e.getMessage());
            } catch (IOException e) {
                e.printStackTrace();
                return TabJsonResult.errorMessage("上传失败," + e.getMessage());
            }
        } else {
            return TabJsonResult.errorMessage("上传失败，因为文件是空的.");
        }

        return TabJsonResult.ok();
    }
}
